package com.example.online_exam_system.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.example.online_exam_system.commons.Result;
import com.example.online_exam_system.logic.IStudentLogicService;
import com.example.online_exam_system.logic.ITeacherLogicService;
import com.example.online_exam_system.service.StudentService;
import com.example.online_exam_system.utils.HAUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description 页面跳转控制器
 * @param
 * @return
 * @author lianglong
 */
@Controller
@Slf4j
public class PageController {


    @Autowired
    ITeacherLogicService iTeacherLogicService;
    @Autowired
    HttpSession session;
    @Autowired
    IStudentLogicService iStudentLogicService;

    @GetMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @GetMapping("/toreg")
    public String register(){
        return "regist";
    }

    @GetMapping("/index")
    public String index(){
        return "index";
    }


    @RequestMapping("page/{page1}")
    public String showPage(@PathVariable("page1") String page1){
        return "page/"+page1;
    }

    @GetMapping("toTeacherLogin")
    public String toTeacherLogin(){
        return "teacherLogin";
    }

    @GetMapping("page/teacherinfo")
    public String  getTeacherInfo(ModelMap modelMap){
        String realname = (String)session.getAttribute("username");
        Map<String, Object> teacherInfo = iTeacherLogicService.getTeacherInfo(realname);
        modelMap.addAllAttributes(teacherInfo);
       return"page/teacherinfo";
    }

    @RequestMapping("/page/modifyteacherInfo")
    public String modifyTeacherInfo(ModelMap modelMap){
        //获取用户姓名
        String realname = (String)session.getAttribute("username");
        //获取全部科目列表
        List<Map<String,Object>> subjectlist = iTeacherLogicService.getAllSubjects();
        //根据姓名获取教师所受科目列表（科目id列表）
        List<Map<String,Object>> list = iTeacherLogicService.getTeaSubIds(realname);
        //根据id列表获取科目名称列表
        List<Integer> teasubids = new ArrayList<>();
        for(Map<String,Object> map:list){
            int sujectid = MapUtils.getInteger(map,"subjectid");
            teasubids.add(sujectid);
        }
        //获取教师基本信息
        Map<String, Object> teacherInfo = iTeacherLogicService.getTeacherInfo(realname);
        modelMap.addAttribute("teacherinfo",teacherInfo);
        modelMap.addAttribute("subjectlist",subjectlist);
        modelMap.addAttribute("teasubids",teasubids);
        return "page/modifyteacherinfo";
    }

    /**
     * @description 添加试卷
     * @param
     * @return
     * @author lianglong
     */
    @GetMapping("/page/addexam")
    public String addExam(ModelMap map){
        //获取两部分信息 教师所选科目名称以及id，以及教师所选科目的试题列表
        String realname = (String)session.getAttribute("username");
        List<String> teaSubNames = iTeacherLogicService.getTeaSubs(realname);
        map.addAttribute("subjects",teaSubNames);
        return "/page/addexam";
    }

    @GetMapping("/page/exampage2/{testid}")
    public String examPage2(@PathVariable("testid") String testId,ModelMap mp ){
        String realname = (String)session.getAttribute("username");
        List<Map<String,Object>> result = iStudentLogicService.ExamDeatials(realname,testId);
        mp.addAttribute("examinfo",result.get(0));
        result.remove(0);
        mp.addAttribute("exams",result);
        log.debug("试卷题数："+(result.size()));
        return "page/exampage2";
    }

    @RequestMapping("page/querystuInfo")
    public String queryStuInfo(ModelMap map){
        List<Map<String, Object>> list = iStudentLogicService.getStudentInfo(session.getAttribute("username").toString());
        map.addAttribute("stuinfo",list.get(0));
        map.addAttribute("subinfo", HAUtils.castList(list.get(1).get("subjectinfo"),Map.class));
        log.debug(list.get(0)+"");
        log.debug(list.get(1)+"");
        return "page/querystuInfo";

    }

    /**
     * 学生修改信息页面
     * @param map
     * @return
     */
    @RequestMapping("/page/modifystuInfo")
    public String modifyStudentInfo(ModelMap map){
        List<Map<String,Object>> list = iStudentLogicService.modifyStudentInfo();
        map.addAttribute("studentinfo",list.get(0));
        log.debug(list.get(0)+"");
        map.addAttribute("subjectinfo",list.get(1).get("subjectinfo"));
        log.debug(list.get(1)+"");
        map.addAttribute("subteas",list.get(2).get("subteainfo"));
        log.debug(list.get(2)+"");
        return "page/modifystuInfo";
    }

    @GetMapping("/page/readpage")
    public String readPage(ModelMap mp){
        List<Map<String, Object>> list = iTeacherLogicService.readPage();
        mp.addAttribute("list",list);
        log.debug(list+"");
        return "page/readpage";
    }

    @GetMapping("/admin")
    public String admin(){
        return "admin/index";
    }

    @RequestMapping("admin/html/{page1}")
    public String showPage2(@PathVariable("page1") String page1){
        return "admin/html/"+page1;
    }

    @RequestMapping("admin/html/device/{page1}")
    public String showPage3(@PathVariable("page1") String page1){
        return "admin/html/device/"+page1;
    }
    @PostMapping("/modifyscore")
    public String modifyscore(@RequestParam Map<String,Object> map){
        log.debug("map:"+map);
        int modifyscore = iTeacherLogicService.modifyscore(map);
        if(modifyscore == 1){
            return "page/readpage";
        }else{
            return null;
        }
    }
    @GetMapping("/examdetail/{userid}/{testid}")
    public String  examDetail(@PathVariable("userid") String userId, @PathVariable("testid") String testId, ModelMap model){
        //试卷名称 试卷总分数 每道题题干 每道题选项 您的答案 你的得分  标准答案 解析
        List<Map<String,Object>> list = iStudentLogicService.ExamDeatials2(userId,testId);
        Map map1 = JSON.parseObject(JSON.toJSONString(list.get(0)), Map.class);
        model.addAttribute("examinfo", map1);
        List<Map> list2 = HAUtils.castList(list.get(1).get("list2"), Map.class);
        model.addAttribute("examdetail",list2);
        log.debug("examinfo:"+map1);
        log.debug("examdetail:"+list2);
        return "page/examdetail";
    }



}
